package com.sdry.transit.service;


import com.sdry.basic.entity.ContainerStare;
import com.sdry.basic.entity.ContainerStareRecord;
import com.sdry.transit.entity.TransitLoading;
import com.sdry.transit.entity.TransitLoadingDetail;
import com.sdry.transit.entity.TransitOrder;
import com.sdry.transit.entity.TransitOrderDetail;
import com.sdry.transit.mapper.TransitLoadingDetailMapper;
import com.sdry.transit.mapper.TransitLoadingMapper;
import com.sdry.transit.mapper.TransitOrderDetailMapper;
import com.sdry.transit.mapper.TransitOrderMapper;
import com.sdry.untils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *@ClassName: TransitOrderServiceImpl
 *@Description: 中转区生产指令
 *@Author tdd
 *@Date 2020-2-19 15:38:03
 *@version 1.0
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TransitOrderServiceImpl implements TransitOrderService {
	@Resource
	TransitOrderMapper transitOrderMapper;
	@Resource
	TransitLoadingMapper transitLoadingMapper;
	@Resource
	TransitLoadingDetailMapper transitLoadingDetailMapper;
	@Resource
	TransitOrderDetailMapper transitOrderDetailMapper;
	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(TransitOrderServiceImpl.class);
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	public TransitOrder queryById(Long id) {
		return transitOrderMapper.queryById(id);
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public List<TransitOrder> queryAllByMution(TransitOrder param) {
		return transitOrderMapper.queryAllByMution(param);
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public List<TransitOrder> findPageByMution(TransitOrder param) {
		return transitOrderMapper.findPageByMution(param);
	}
	/** 
	 * 条件查询记录数
	 * @param param 实体条件
	 * @return 实体集合
	*/
	public Integer selectCountByMution(TransitOrder param) {
		return transitOrderMapper.selectCountByMution(param);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @return 主键id
	*/
	public Long insert(TransitOrder param) {
		return transitOrderMapper.insert(param);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @return 影响行数
	*/
	public Integer update(TransitOrder param) {
		return transitOrderMapper.update(param);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	public Integer delete(String ids) {
		return transitOrderMapper.delete(ids);
	}

	/**
	 * 添加装车信息
	 * @param containerStareRecords
	 * @param cid 车辆id
	 * @param did 司机id
	 * @param username 登录人
	 * @return
	 */
	public Long addTransitLoading(List<ContainerStareRecord> containerStareRecords, Long cid, Long did,String username) {
		TransitLoading transitLoading = new TransitLoading();
		transitLoading.setCid(cid);
		transitLoading.setDid(did);
		transitLoading.setTlcreateTime(DateUtil.dateFormat3());
		transitLoading.setTlcreateMan(username);
		Long tlid = transitLoadingMapper.insert(transitLoading);
		if(null != tlid && tlid > 0) {
			TransitLoadingDetail transitLoadingDetail = new TransitLoadingDetail();
			transitLoadingDetail.setTlid(tlid);
			transitLoadingDetail.setTldcreateTime(DateUtil.dateFormat3());
			transitLoadingDetail.setTldcreateMan(username);
			for (int i = 0; i < containerStareRecords.size(); i++) {
				transitLoadingDetail.setCsid(containerStareRecords.get(i).getCsrid());
				List<TransitLoadingDetail> transitLoadingDetails = transitLoadingDetailMapper.queryAllByMution(transitLoadingDetail);
				//判断重复数据
				if(null == transitLoadingDetails || transitLoadingDetails.size() == 0){
					transitLoadingDetailMapper.insert(transitLoadingDetail);
				}
			}
		}
		return tlid;
	}

	/**
	 * 添加生产指令（包括明细）
	 * @param param
	 * @param mids 物流ID
	 * @param todnums 数量
	 * @param todSprayingRequirements 喷涂要求
	 * @param todSpecialRequirements 特殊要求
	 * @param urgent 紧急程度
	 * @return
	 */
	public Long insertPl(TransitOrder param,Long[] mids,int[] todnums
			,String[] todSprayingRequirements
			,String[] todSpecialRequirements
			,String[] urgent,String[] todremark,String[] todremark1) {
		Long id = transitOrderMapper.insert(param);
		if(null != id && id > 0){
			for (int i = 0; i < mids.length; i++) {
				TransitOrderDetail transitOrderDetail = new TransitOrderDetail();
				transitOrderDetail.setToid(id);
				transitOrderDetail.setMid(mids[i]);
				transitOrderDetail.setTodnum(todnums[i]);
				transitOrderDetail.setTodSprayingRequirements(todSprayingRequirements[i]);
				transitOrderDetail.setTodSpecialRequirements(todSpecialRequirements[i]);
				transitOrderDetail.setUrgent(urgent[i]);
				transitOrderDetail.setTodremark(todremark[i]);
				transitOrderDetail.setTodremark1(todremark1[i]);
				transitOrderDetail.setWorkshop("华强");
				transitOrderDetailMapper.insert(transitOrderDetail);
			}
		}
		return id;
    }
}
